package com.mystudy.springcloud.controller;

import com.mystudy.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {
	@Autowired
	private PaymentHystrixService paymentHystrixService;

	/**
	 * @功能: 消费端调用服务端不会出问题的方法
	 */
	@GetMapping("/consumer/payment/hystrix/ok/{id}")
	public String peymentInfo_OK(@PathVariable("id") Integer id) {
		String infoOk = paymentHystrixService.peymentInfo_OK(id);
		return infoOk;
	}

	/**
	 * @功能: 消费端调用服务端可能会出问题的方法
	 */
	@GetMapping("/consumer/payment/hystrix/timeout/{id}")
	@HystrixCommand(fallbackMethod = "payment_TimeOutHandler", commandProperties = {
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
	})
	public String payment_TimeOut(@PathVariable("id") Integer id) {
		String timeOut = paymentHystrixService.payment_TimeOut(id);
		return timeOut;
	}

	/**
	 * @功能: 可能会出问题的方法出问题后兜底的方法
	 */
	public String payment_TimeOutHandler(@PathVariable("id") Integer id) {
		return "线程池：" + Thread.currentThread().getName() + " payment_TimeOutHandler " + id + " (┭┮﹏┭┮) " + "\t当前业务繁忙，请稍后再试！- 80";
	}

	/**
	 * @功能: 全局fallback方法
	 */
	public String payment_Global_FallbackMethod() {
		return "Global异常处理信息，请稍后再试，（┭┮﹏┭┮）";
	}
}
